<template>
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <!--  循环渲染轮播图的item项 -->
      <swiper-item v-if="swiperList!==''" v-for="(item,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+ item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view v-if="navList!==''" class="nav-item" v-for="(item,index) in navList" :key="index"
        @click="navClickHandel(item)">
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 渲染楼层 -->
    <view class="floor-list">
      <view v-if="floorList!==''" class="floor-item" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层的标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>


        <!-- 渲染楼层里的图片 -->
        <view class="floor-img-box">
          <!-- 左侧大盒子 -->
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix">
            </image>
          </navigator>
          <!-- 右侧小盒子 -->
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(item2,index2) in item.product_list" :key="index2"
              :url="item2.url">
              <image v-if="index2!==0" :src="item2.image_src" :style="{width:item2.image_width+'rpx'}" mode="widthFix">
              </image>
            </navigator>

          </view>
        </view>

      </view>
    </view>




  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabBar-badge.js'
  export default {
    mixins: [badgeMix],

    data() {
      return {
        //1.轮播图的数据列表。默认为空数组
        swiperList: [],
        // 分类列表
        navList: [],
        //楼层列表
        floorList: []
      }
    },
    onLoad() {

      // 2.在小程序页面刚加载的时候，调用获取轮播图数据的方法
      this.getSwiperList()

      this.getNavList(),
        this.getFloorList()

    },
    methods: {


      //1. 获取轮播图数据的方法
      async getSwiperList() {

        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }

        this.swiperList = res.message
      },

      //3.在methds中定义获取数据的方法
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
        //console.log(this.navList)
      },
      async getFloorList() {

        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return



        // 通过双层forEach循环，处理URL地址
        // 获取楼层数据请求成功之后，通过双层forEach循环，处理url地址：
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
            //  console.log(prod.url)
          })
        })
        this.floorList = res.message
      },
      // nav-item项被点击时候的事件处理函数
      navClickHandel(item) {

        // 判断点击的是那个nav
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
        if (item.name === '秒杀拍') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }

        if (item.name === '超市购') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
        if (item.name === '母婴品') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 去搜索页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }

    }

  }
</script>

<style lang="scss">
  //轮播图
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  // 分类
  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  //楼层
  .floor-title {
    width: 100%;
    height: 60rpx;
  }

  // 楼层里的图片
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }

  .search-box {
    // 设置定位效果为“吸顶”
    position: sticky;
    // 吸顶的“位置”
    top: 0;
    // 提高层级，防止被轮播图覆盖
    z-index: 999;
  }
</style>